import json

from django.http import HttpResponse, JsonResponse, HttpResponseForbidden
from django.shortcuts import redirect

def index(request):
    return HttpResponse("Hello Word")

def index1(request):
    return HttpResponse('11111111')

def index2(request,city, year):
    return HttpResponse("城市名：%s,年份：%s" %(city,year))

def get_view(request):
    name = request.GET.get('name')
    names = request.GET.getlist('name')
    age = request.GET.get('age')
    return HttpResponse('get:%s,%s,%s'%(name,names,age))

def post_view(request):
    #  表单
    # name = request.POST.get('name')
    # age = request.POST.get('age')
    # 1.拿到bodyjson数据
    body_data = request.body.decode()  # body是bites类型，需要转码
    # 2.解析json数据
    body_dict = json.loads(body_data)
    # 3.获取数据
    name = body_dict.get('name')
    age = body_dict.get('age')
    return HttpResponse('%s,%s'%(name,age))

def put_view(request):
    # 1.获取body并decode
    body_json = request.body.decode()
    # 2.把json转成字典
    body_dict = json.loads(body_json)
    # 3.从字典中获取数据
    name = body_dict.get('name')
    age = body_dict.get('age')
    return HttpResponse("%s,%s"%(name,age))

def delete_view(request):
    # 1.获取body并decode
    body_json = request.body.decode()
    # 2.把json转成字典
    body_dict = json.loads(body_json)
    # 3.从字典中获取数据
    name = body_dict.get('name')
    age = body_dict.get('age')
    return HttpResponse("delete:%s,%s"%(name,age))

def request_view(request):
    print(request.method)
    print(request.user)
    print(request.COOKIES)
    return HttpResponse('request')

def response_view(request):
    response = HttpResponse()
    response.content = "这是内容"
    response.status_code = 200
    response['Content-Type'] = 'text/html;charset=utf-8' \
    # response['Content-Type'] = 'abc'
    return response

def json_response_view(request):
    d = {
        "name":'zhangsan',
        "age": 20
    }
    ds = [
        {
            "name":'zhangsan',
            "age": 10
        },
        {
            "name": 'lisi',
            "age": 15
        }
    ]
    return JsonResponse(ds, safe=False)
    # d_json = json.dumps(d)
    # resp = HttpResponse(d_json)
    # resp['Content-type'] = 'ap.../json'
    # return resp
    # return HttpResponse(d_json)

def forbidden_view(request):
    return HttpResponseForbidden('拒绝访问')

def redirect_view(request):
    # return redirect('app01/index/')
    resp = HttpResponse()
    resp.status_code = 302
    resp['Location'] = '/app01/index/'
    return resp

def user_center_view(request):
    url = reversed('app01:login')
    print(url)
    return redirect(url)

def login_view(request):
    return HttpResponse('登录成功')

def cookie_view(request):
    resp = HttpResponse('set cookie')
    resp.set_cookie('name','lisi',max_age=30)
    return resp

def get_cookie_view(request):
    name = request.COOKIES.get('name')
    return HttpResponse("name:%s" % name)

def set_session_view(request):
    request.session['name'] = 'lisi'
    request.session['age']=18
    return HttpResponse('set session')
def get_session_view(request):
    name = request.session.get('name')
    return HttpResponse(name)

def del_session_view(request):
    request.session.pop('name')
    del request.session['age']
    return HttpResponse('del session')

def flush_session_view(request):
    request.session.flush()
    return HttpResponse('flush')

